package com.weicx.wuzhiops.controller;

import com.weicx.wuzhiops.annotation.EventLog;
import com.weicx.wuzhiops.annotation.RestWrapper;
import com.weicx.wuzhiops.constant.event.EventType;
import com.weicx.wuzhiops.model.request.UserLoginRequest;
import com.weicx.wuzhiops.service.AuthenticateService;
import com.weicx.wuzhiops.util.ServletsUtil;
import com.weicx.wuzhiops.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author:phj
 * @date: 2022-12-06 10:28:03
 * @since: 1.0
 */
@Api(value = "用户认证登录", tags = {"用户认证登录"})
@RestController
@RestWrapper
@RequestMapping(ServletsUtil.BASE_REQUEST_PATH_PREFIX + "/auth")
public class AuthenticateController {


    @Resource
    private AuthenticateService authenticateService;


    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @EventLog(value = EventType.LOGIN)
    public UserLoginVO login(@RequestBody @Validated UserLoginRequest request, HttpServletRequest req) {

        System.out.println("request = " + request + ", req = " + req);
        String ip = ServletsUtil.getRemoteAddr(req);
        request.setUsername(request.getUsername().trim());
        request.setPassword(request.getPassword().trim());
        request.setIp(ip);

        return authenticateService.login(request);
    }

}
